/**
 * 
 */
package com.idea.auction.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.idea.auction.constant.Const;
import com.idea.auction.model.Admin;
import com.idea.auction.model.Dictionary;
import com.idea.auction.model.Permission;
import com.idea.auction.model.Commodity;
import com.idea.auction.service.CommodityService;
import com.idea.auction.service.DictionaryService;
import com.idea.auction.service.PermissionService;
import com.idea.auction.util.SysUtil;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/com",viewPath="/pages")
public class CommodityController extends BaseController {
	private static final Logger log = Logger.getLogger(CommodityController.class);
	@Service
	private CommodityService commodityService;
	@Service
	private DictionaryService dictionaryService;
	/**
	 * 跳转到商品页
	 */
	@RequiresPermissions("com/toCommodity")
	public void toCommodity(){
		getNavigation();
		render("commodity/commodity-list.html");
	}
	
	/**
	 * 商品编辑页面
	 */
	public void toEditCommodity(){
		//getNavigation();
		Commodity commodity = new Commodity();
		
		if(!StrKit.isBlank(this.getPara("id"))){
			commodity = commodityService.getCommodityById(this.getPara("id"));
		}
		//对象
		setAttr("commodity", commodity);
		
		//获取款式
		List<Dictionary> styleList= dictionaryService.getListByCode("style");
		setAttr("styleList", styleList);
		
		//获取图案
		List<Dictionary> pattenList= dictionaryService.getListByCode("patten");
		setAttr("pattenList", pattenList);
		
		render("commodity/commodity-edit.html");
	}
	
	/**
	 * 获取商品分页
	 */
	public void getCommodityList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		
		String commodityName = getPara("commodityName");
		if(StrKit.isBlank(commodityName)){
			commodityName = "";
		}
		super.setPage(commodityService.getPage(curPage,commodityName));
	}
	
	/**
	 * 添加商品
	 */
	public void addCommodity(){
		String picture = super.uploadImg("picture");
		String first_picture = super.uploadImg("first_picture");
		String second_picture = super.uploadImg("second_picture");
		String thrid_picture = super.uploadImg("thrid_picture");
		String fourth_picture = super.uploadImg("fourth_picture");
		String fifth_picture = super.uploadImg("fifth_picture");
		String sixth_picture = super.uploadImg("sixth_picture");
		
		Commodity commodity = new Commodity();
		Admin admin = (Admin) getSession().getAttribute("admin");
		
//		commodity.set("id", SysUtil.getUUID());
		commodity.set("commodity_name", this.getPara("commodity_name"));
		commodity.set("specification", this.getPara("specification"));
		commodity.set("style", this.getPara("style"));
		commodity.set("patten", this.getPara("patten"));
		commodity.set("origin", this.getPara("origin"));
		commodity.set("describes", this.getPara("describes"));
		commodity.set("start_date_time", this.getPara("start_date_time"));
		commodity.set("end_date_time", this.getPara("end_date_time"));
		commodity.set("start_price", this.getPara("start_price"));
		commodity.set("add_price", this.getPara("add_price"));
		commodity.set("deal_price", this.getPara("deal_price"));
		commodity.set("collection_num", this.getPara("collection_num"));
		commodity.set("browse_num", this.getPara("browse_num"));
		commodity.set("sort", this.getPara("sort"));
		commodity.set("update_name", admin.get("name"));
		commodity.set("update_date", new Date());
		commodity.set("create_name", admin.get("name"));
		commodity.set("create_date", new Date());
		if(!StrKit.isBlank(picture)){
			commodity.set("picture", picture);
		}
		if(!StrKit.isBlank(first_picture)){
			commodity.set("first_picture", first_picture);
		}
		if(!StrKit.isBlank(second_picture)){
			commodity.set("second_picture", second_picture);
		}
		if(!StrKit.isBlank(thrid_picture)){
			commodity.set("thrid_picture", thrid_picture);
		}
		if(!StrKit.isBlank(fourth_picture)){
			commodity.set("fourth_picture", fourth_picture);
		}
		if(!StrKit.isBlank(fifth_picture)){
			commodity.set("fifth_picture", fifth_picture);
		}
		if(!StrKit.isBlank(sixth_picture)){
			commodity.set("sixth_picture", sixth_picture);
		}
		if(commodityService.add(commodity)){
			msgAdd();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
		
	}
	
	/**
	 * 编辑商品
	 */
	public void editCommodity(){
		String picture = super.uploadImg("picture");
		String first_picture = super.uploadImg("first_picture");
		String second_picture = super.uploadImg("second_picture");
		String thrid_picture = super.uploadImg("thrid_picture");
		String fourth_picture = super.uploadImg("fourth_picture");
		String fifth_picture = super.uploadImg("fifth_picture");
		String sixth_picture = super.uploadImg("sixth_picture");
		
		Commodity commodity = new Commodity();
		Admin admin = (Admin) getSession().getAttribute("admin");
		commodity.set("id", this.getPara("id"));
		commodity.set("commodity_name", this.getPara("commodity_name"));
		commodity.set("specification", this.getPara("specification"));
		commodity.set("style", this.getPara("style"));
		commodity.set("patten", this.getPara("patten"));
		commodity.set("origin", this.getPara("origin"));
		commodity.set("describes", this.getPara("describes"));
		commodity.set("start_date_time", this.getPara("start_date_time"));
		commodity.set("end_date_time", this.getPara("end_date_time"));
		commodity.set("start_price", this.getPara("start_price"));
		commodity.set("add_price", this.getPara("add_price"));
		commodity.set("deal_price", this.getPara("deal_price"));
		commodity.set("collection_num", this.getPara("collection_num"));
		commodity.set("browse_num", this.getPara("browse_num"));
		commodity.set("sort", this.getPara("sort"));
		commodity.set("update_name", admin.get("name"));
		commodity.set("update_date", new Date());
		commodity.set("create_name", admin.get("name"));
		commodity.set("create_date", new Date());
		if(!StrKit.isBlank(picture)){
			commodity.set("picture", picture);
		}else{
			commodity.set("picture", "");
		}
		
		if(!StrKit.isBlank(first_picture)){
			commodity.set("first_picture", first_picture);
		}else{
			commodity.set("first_picture", "");
		}
		
		if(!StrKit.isBlank(second_picture)){
			commodity.set("second_picture", second_picture);
		}else{
			commodity.set("second_picture", "");
		}
		
		if(!StrKit.isBlank(thrid_picture)){
			commodity.set("thrid_picture", thrid_picture);
		}else{
			commodity.set("thrid_picture", "");
		}
		
		if(!StrKit.isBlank(fourth_picture)){
			commodity.set("fourth_picture", fourth_picture);
		}else{
			commodity.set("fourth_picture", "");
		}
		
		if(!StrKit.isBlank(fifth_picture)){
			commodity.set("fifth_picture", fifth_picture);
		}else{
			commodity.set("fifth_picture", "");
		}
		
		if(!StrKit.isBlank(sixth_picture)){
			commodity.set("sixth_picture", sixth_picture);
		}else{
			commodity.set("sixth_picture", "");
		}
		
		if(commodityService.add(commodity)){
			msgAdd();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
		
		if(commodityService.update(commodity)){
			msgUpdate();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
		
	}
	
	/**
	 * 删除商品
	 */
	public void delCommodity(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			commodityService.delete(id);
		}
		
		msgSuccess();
	}
	
	
}	